Skip to content

[lln_clt.md] Update np.random → Generator API#874

Merged
jstac merged 1 commit into
mainfrom
update-rng-lln-clt
May 28, 2026
Merged

[lln_clt.md] Update np.random → Generator API#874
jstac merged 1 commit into
mainfrom
update-rng-lln-clt

Conversation

@Chihiro2000GitHub
Copy link
Copy Markdown
Contributor

Summary

This PR migrates legacy NumPy random API usage in lln_clt.md as part of QuantEcon/meta#299.

The single legacy call np.random.randint inside gen_x_draws has been replaced with rng.integers from a np.random.default_rng() generator.

Details

rng is introduced as an explicit required argument to gen_x_draws(k, rng), and rng = np.random.default_rng() is defined in the same cell just before the call site. Since rng is always available at the point of use, no rng=None fallback was added.

This differs from the approach taken in a related PR (lecture-python-intro#741), where rng was created once at the top of the lecture and reused across multiple cells — so functions there could be called from contexts where rng was not always explicitly passed, making the if rng is None fallback reasonable. In this lecture, rng is created in the same cell as the function definition and its call site, so the fallback seemed unnecessary.

I'd welcome your thoughts on whether this difference in approach is appropriate, or whether a consistent rng=None pattern should be preferred across lectures.

Hi @mmcky and @HumphreyYang, I'd be grateful if you could take a look when you have time.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

📖 Netlify Preview Ready!

Preview URL: https://pr-874--sunny-cactus-210e3e.netlify.app

Commit: 6817071

📚 Changed Lectures


Build Info

@jstac
Copy link
Copy Markdown
Contributor

jstac commented May 28, 2026

Thanks @Chihiro2000GitHub

@jstac jstac merged commit 5175665 into main May 28, 2026
1 check passed
@jstac jstac deleted the update-rng-lln-clt branch May 28, 2026 19:28
thomassargent30 pushed a commit that referenced this pull request May 31, 2026
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
HumphreyYang added a commit that referenced this pull request Jun 6, 2026
* Tom's May 8 edits of new lecture

* Tom's April 9 edits of new and old lectures

* Tom's addition of a lecture

* updates

* updates

* updates

* updates

* updates

* updates

* updates

* updates

* updates

* updates

* Update rng usage in lln_clt.md (#874)

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* Fix assorted issues in prob_meaning lecture (#878)

* Fix typos, spelling, notation errors, and swapped bounds in prob_meaning

- Fix spelling: probabilties, probabililty (x6), statististian
- Fix doubled word: "to to help"
- Fix variance formula: remove erroneous factor of n (rho is Bernoulli, not binomial)
- Fix notation: P_{k,i} → rho_{k,i} to match definition
- Fix subject-verb agreement: "means converges" → "mean converges"
- Fix swapped upper/lower bounds in part (e) ppf calls
- Fix compare() to include k=0
- Fix LaTeX: replace * with \cdot for multiplication
- Fix log(I) range: text said 2 to 7, code has 2 to 6
- Fix imprecise wording: f_k^I approximates Prob(X=k|θ), not θ
- Clarify vague exercise pm_ex1 part 3

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Improve code quality: naming, PEP 8, line length, grid resolution

- Rename class frequentist → Frequentist (PEP 8)
- Rename Bay_stat → bayes (snake_case for instances)
- Rename ii → i/post, num → n_obs, num_list → n_obs_list,
  kk → k, K → head_counts, comp → table, step_num → n_obs,
  npt → n_thetas, nn → n_ns, nI → n_Is
- Replace (sample <= θ) * 1 with .astype(int) for consistency
- Shorten docstrings to fit within 80 characters
- Break long code lines (plot calls, list comprehensions, titles)
- Increase θ grid from 100 to 1000 points for smoother density plots
- Use f-strings with comma formatting for plot labels

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Restructure Bayesian section: derive posterior before exercise

- Add back-reference to prob_matrix lecture for Bayes' Law intro
- Derive the n-step posterior Beta(α+k, β+n-k) before the exercise,
  so the exercise solution code no longer precedes its own derivation
- Replace the duplicated derivation after the exercise with a concise
  summary referencing the formula above
- Remove duplicate "Now pretend..." sentence before part (c)
- Replace "this quantecon lecture" cross-references with actual titles
  for better PDF rendering

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Replace prettytable with pandas DataFrame in compare()

Drop the prettytable dependency — pandas is already imported and
renders nicely in Jupyter notebooks. The compare() method now returns
a DataFrame instead of printing a PrettyTable.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Fix exercise pm_ex2 parts (a) and (b) to match their solutions

The question asked for the likelihood of "a sample of length n from
a binomial" but the solution gave the single-flip Bernoulli case.
Reword both the questions and solution headers so parts (a) and (b)
are explicitly about a single coin flip. The general n-step case is
already derived in the lecture text before the exercise.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Add reproducible random seeds using modern NumPy API

Both classes now accept an rng parameter and use rng.random()
instead of np.random.rand(). Each code cell passes a seeded
np.random.default_rng() for reproducible output across builds.

Also remove "typically" from the hump-shape sentence, since with
fixed seeds the behavior is deterministic.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Clean up posterior PDF plots

- Rename title from "P.D.F" to "PDF"
- Simplify legend labels: "n = 0 (prior)", "n = 1", etc.
- Remove n = 30, 70, 300, 500 from observation list to reduce
  clutter in the first PDF plot

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Remove repeated question text from exercise solutions

Solutions for parts (c)-(h) no longer duplicate the question text
as a header — they just use the part label. This follows the
QuantEcon convention.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Use p(θ) for density notation and break up solution derivation

- Change P(θ) to p(θ) throughout and note it is a density
- Replace the single aligned equation block in the solution for
  part (b) with three separate display equations, each introduced
  by explanatory text (Bayes' Law, substitution, collecting powers)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Final review polish: notation, plotting style, consistency

- Use **bold** instead of __bold__ for binomial distribution
- Use IID instead of i.i.d.
- Fix double "with" in exercise (c) wording
- Rename quantile variables from p_1/p_2 to q_1/q_2 to avoid
  clash with p(θ) density notation
- Fix "means and variances statistics" → "mean and standard deviation"
- Standardize N → n in post-exercise text to match pre-exercise
- Update "exceeds 500" → "exceeds 1000" to match revised n_obs_list
- Standardize frequentist plots to use ax. methods instead of plt.
- Remove dead self.k assignment in Frequentist.binomial()

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Adopt new style guide notation conventions

Replace \textrm{Prob}(...) with \mathbb{P}{...} and E[...] with
\mathbb{E}[...] following QuantEcon.manual#84.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Update rng usage in stats_examples.md (#873)

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* Tom's May 31 edits of long-run risk lecture

* updates

* updates

* updates

* updates

* updates

* updates

* Tom's June 5 edits of long run risk model

* update

---------

Co-authored-by: thomassargent30 <ts43@nyu.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants